# coding:utf-8
import os

import numpy as np
import tensorflow as tf
import cv2 as cv
"""
# Load TFLite model and allocate tensors.
tflite_model = tf.lite.Interpreter(
    model_path=r"C:\\Users\Administrator\Desktop\zzaq_20190724_1520.tflite")
tflite_model.allocate_tensors()
PATH_TO_TEST_IMAGES_DIR = r'C:\\Users\Administrator\Desktop\\ng'
TEST_IMAGE_PATHS = [os.path.join(PATH_TO_TEST_IMAGES_DIR, i) for i in os.listdir(PATH_TO_TEST_IMAGES_DIR)]

# Get input and output tensors.
input_details = tflite_model.get_input_details()
output_details = tflite_model.get_output_details()

# Test model on random input data.
input_shape = input_details[0]['shape']
print(input_shape)
for image_path in TEST_IMAGE_PATHS:
    image = cv.imread(image_path)

    input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)  # 输入随机数
    print('input_data',input_data.shape)
    input_data[0]=image
    tflite_model.set_tensor(input_details[0]['index'], input_data)

    tflite_model.invoke()
    output_data = tflite_model.get_tensor(output_details[0]['index'])
    print("out_class")
    print(output_data)



"""
"""
获取输入输出tensor
获取输入shape
设置一个占位符array
填充
set_tensor: 在input_tensor index是我传入的值
唤醒
get_tensor: 从outputtensor
"""
def example():
    # !/usr/bin/python
    # coding:utf-8
    ROWS = 151
    COLS = 72
    CHANNELS = 1
    # duiying = ['32_无线_蜂窝', '128_无线_蜂窝',
    #            '32_wifi_cellular', '128_wifi_cellular',
    #            '32_无线局域网', '128_无线局域网',
    #            '32_wifi', '128_wifi']
    candidate = ['ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT']

    def tflite_test():
        import os
        from scipy import misc
        import numpy as np
        import tensorflow as tf

        # Load TFLite model and allocate tensors.
        tflite_model = tf.lite.Interpreter(
            model_path=r"C:\Users\Administrator\Desktop\zkkz_20190725_1100.tflite")
        tflite_model.allocate_tensors()
        PATH_TO_TEST_IMAGES_DIR = r''
        TEST_IMAGE_PATHS = [os.path.join(PATH_TO_TEST_IMAGES_DIR, i) for i in os.listdir(PATH_TO_TEST_IMAGES_DIR)]

        # Get input and output tensors.
        input_details = tflite_model.get_input_details()
        output_details = tflite_model.get_output_details()

        # Test model on random input data.
        input_shape = input_details[0]['shape']
        # print('\n\n\n',input_shape,input_details)
        for image_path in TEST_IMAGE_PATHS:
            img = misc.imread(image_path)
            img = misc.imresize(img, (ROWS, COLS))
            # img = np.expand_dims(img, axis=2)

            input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)  # 输入随机数
            # print('input_data', input_data.shape)
            input_data[0] = img
            tflite_model.set_tensor(input_details[0]['index'], input_data)

            tflite_model.invoke()
            output_data = tflite_model.get_tensor(output_details[0]['index'])
            # print("out_class")
            maxindex = np.argmax(output_data)
            print(image_path)
            print(maxindex, '\t', '\n\n')
    tflite_test()
example()